import VariablePool
import ThreadPool
from Common import *
from threading import *
import time

class Collector:
    def __init__(self):
        pass
    
    def Start(self):
        ThreadPool.threadpool.AddTask(self.Run)
    
    def Stop(self):
        raise NotImplementedError("Method Collector::Stop not implemented.")
    
    def Run(self):
        raise NotImplementedError("Method Collector::Run not implemented.")
    
class CyclicCollector(Collector):
    def __init__(self, interval = 1):
        Collector.__init__(self)
        self.interval = interval
        self.alive = False
        
    def SetInterval(self, interval):
        self.interval = interval
        
    def GetInterval(self):
        return self.interval
    
    def IsAlive(self):
        return self.alive
        
    def Start(self):
        self.alive = True
        ThreadPool.threadpool.AddTask(self.Run)
        
    def Stop(self):
        if not self.alive:
            return
        self.alive = False
        
    def Run(self):
        while self.alive:
            try:
                self.OneCyclic()
            except:
                self.alive = False
                raise
            time.sleep(self.interval)
            
    def OneCyclic(self):
        raise NotImplementedError("Method CyclicCollector::OneCyclic not implemented.")
    
collmanager = None

class CollectorManager:
    def __init__(self):
        self.collectors = []
        
    def AddCollector(self, obj):
        obj.Start()
        self.collectors.append(obj)
    
    def WaitComplete(self):
        for obj in self.collectors:
            obj.Stop()